Stack implementation
实现一个 stack 可以用两种数据结构,array(dynamic or fixed) 或者是 linked list。
dynamic array 的优势是支持 random access,因为可以通过 index 获取数据,然而 stack 主要作用是 pop,所以 dynamic array 的这个优势 gains you little。dynamic array 另一个优势是 resize,这个非常的 time-consuming 因为需要 copy array to a new one。
linked list 会为每一个元素分配内存,这比 dynamic array 的 resize 更费时,因此基于 dynamic array 的 stack 通常要比基于 linked list 的 stack 快一些。然而,基于 linked list 的 stack 更容易实现。
Proper functionality
基本方法:
- push
allocate new element, checks for failure, sets the data of the new element, places it at the top of the stack, adjust the stack pointer - pop
check the stack isn’t empty, fetches data from top element, adjusts the stack pointer, free the element that is no longer on the stack
完整方法:
- createStack
push a null pointer - deleteStack
call pop repeatedly
Error handling
- pop
如果 stack 为空,返回 null? 问题是需要保证 stack 里没有存 null pointer;返回 special value(or negative value)?问题是需要 assume stack 里没有这些元素。感觉 raise error 比较简单。 - push
如果传进去的值为 null,raise error
|
|
|
|
Queue implementation
|
|
Leetcode 实例
232.Implement Queue using Stacks
Problem
Implement the following operations of a queue using stacks.
push(x) – Push element x to the back of queue.
pop() – Removes the element from in front of queue.
peek() – Get the front element.
empty() – Return whether the queue is empty.
Notes:
You must use only standard operations of a stack – which means only push to top, peek/pop from top, size, and is empty operations are valid.
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
Solution
|
|
225.Implement Stack using Queues
Problem
Implement the following operations of a stack using queues.
push(x) – Push element x onto stack.
pop() – Removes the element on top of the stack.
top() – Get the top element.
empty() – Return whether the stack is empty.
Notes:
You must use only standard operations of a queue – which means only push to back, peek/pop from front, size, and is empty operations are valid.
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.
用两个队列,push: O(n),pop: O(1),top: O(1)
Solution
|
|
155. Min Stack
Problem
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
push(x) – Push element x onto stack.
pop() – Removes the element on top of the stack.
top() – Get the top element.
getMin() – Retrieve the minimum element in the stack.
Example:
Solution
|
|
20.Valid Parentheses
Problem
Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid.
The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not.
Solution
|
|
150. Evaluate Reverse Polish Notation
Problem
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +, -, *, /. Each operand may be an integer or another expression.
Some examples:
[“2”, “1”, “+”, “3”, “*“] -> ((2 + 1) * 3) -> 9
[“4”, “13”, “5”, “/“, “+”] -> (4 + (13 / 5)) -> 6
Solution
|
|
Followup
71. Simplify Path
Problem
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = “/home/“, => “/home”
path = “/a/./b/../../c/“, => “/c”
click to show corner cases.
Corner Cases:
Did you consider the case where path = “/../“?
In this case, you should return “/“.
Another corner case is the path might contain multiple slashes ‘/‘ together, such as “/home//foo/“.
In this case, you should ignore redundant slashes and return “/home/foo”.
Solution
|
|
84. Largest Rectangle in Histogram
Problem
Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].
The largest rectangle is shown in the shaded area, which has area = 10 unit.
For example,
Given heights = [2,1,5,6,2,3],
return 10.
Solution
|
|
Python stack & deque
这篇用到的 python 的知识点/需要注意的地方。
use lists as stacks
LIFO
add, use append()
retrieve, use pop()
use lists as queues
FIFO,python list 作 queue 并不 efficent,用 collections.deque